﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using MySql.Data.MySqlClient;

namespace YJKDataBase
{
	public class MySqlObjectTable : IObjectTable
	{
		public MySqlObjectTable(RelationDatabase db)
		{
			this.mDatabase = db;
		}

		public bool Add(DbCategory category, DbObject dbObject, DbTransaction tr)
		{
			bool result = false;
			byte[] value = null;
			if (dbObject.Image != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				dbObject.Image.Save(memoryStream, ImageFormat.Jpeg);
				value = memoryStream.ToArray();
			}
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			try
			{
				using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
				{
					mySqlCommand.Transaction = (MySqlTransaction)tr;
					int num;
					if (dbObject.Limit)
					{
						num = 1;
					}
					else
					{
						num = 0;
					}
					mySqlCommand.CommandText = string.Format("insert into ObjectTable(Name,Img,ParentId,`Limit`,`Version`,Description,GUID,Date,eID1,eID2,tag1,tag2) values ('{0}',{1},'{2}','{3}','{4}','{5}','{6}','{7}','{8}',{9},'{10}','{11}')", new object[]
					{
						dbObject.Name,
						"?img",
						category.Guid.ToString(),
						num,
						dbObject.Version,
						dbObject.Description,
						dbObject.Guid.ToString(),
						dbObject.Date.ToString(),
						dbObject.ExData.Eid1,
						dbObject.ExData.Eid2,
						dbObject.ExData.Tag1,
						dbObject.ExData.Tag2
					});
					MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
					mySqlParameter.Value = value;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
					dbObject.SetDatabase(this.mDatabase);
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public int Count(Parent parent)
		{
			if (this.mDatabase.dbCon.State == ConnectionState.Closed)
			{
				this.mDatabase.dbCon.Open();
			}
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select GUID from ObjectTable where ParentId='{0}'", parent.Guid.ToString()));
			if (this.mDatabase.dbCon.State == ConnectionState.Open)
			{
				this.mDatabase.dbCon.Close();
			}
			return dataTable.Rows.Count;
		}

		public int Count()
		{
			if (this.mDatabase.dbCon.State == ConnectionState.Closed)
			{
				this.mDatabase.dbCon.Open();
			}
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select GUID from ObjectTable", Array.Empty<object>()));
			if (this.mDatabase.dbCon.State == ConnectionState.Open)
			{
				this.mDatabase.dbCon.Close();
			}
			return dataTable.Rows.Count;
		}

		public bool Update(DbObject family)
		{
			int num = 0;
			if (family.Limit)
			{
				num = 1;
			}
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			if (mySqlConnection.State == ConnectionState.Closed)
			{
				mySqlConnection.Open();
			}
			bool result;
			using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
			{
				mySqlCommand.CommandText = string.Format("update ObjectTable set Name='{0}',ParentId='{1}',`Limit`='{2}',`Version`='{3}',Description='{4}',Img={5},eID1={6},eID2={7},tag1='{8}',tag2='{9}' where GUID='{10}'", new object[]
				{
					family.Name,
					family.ParentId.ToString(),
					num,
					family.Version,
					family.Description,
					"?img",
					family.ExData.Eid1,
					family.ExData.Eid2,
					family.ExData.Tag1,
					family.ExData.Tag2,
					family.Guid.ToString()
				});
				MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
				byte[] value = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					family.Image.Save(memoryStream, ImageFormat.Jpeg);
					value = memoryStream.ToArray();
				}
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				if (mySqlCommand.ExecuteNonQuery() != -1)
				{
					if (mySqlConnection.State == ConnectionState.Open)
					{
						mySqlConnection.Close();
					}
					result = true;
				}
				else
				{
					if (mySqlConnection.State == ConnectionState.Open)
					{
						mySqlConnection.Close();
					}
					result = false;
				}
			}
			return result;
		}

		public void ReDateByNow(DbObject family)
		{
			string arg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			if (mySqlConnection.State == ConnectionState.Closed)
			{
				mySqlConnection.Open();
			}
			using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
			{
				mySqlCommand.CommandText = string.Format("update ObjectTable set Date='{0}' where GUID='{1}'", arg, family.Guid.ToString());
				mySqlCommand.ExecuteNonQuery();
			}
			if (mySqlConnection.State == ConnectionState.Open)
			{
				mySqlConnection.Close();
			}
		}

		public DbObject this[Guid id]
		{
			get
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where GUID='" + id.ToString() + "'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[0]["Img"]));
					DbObject dbObject = new DbObject(dataTable.Rows[0]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[0]["GUID"].ToString()));
					dbObject.Limit = Convert.ToBoolean(dataTable.Rows[0]["Limit"].ToString());
					dbObject.Version = dataTable.Rows[0]["Version"].ToString().Trim();
					dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[0]["Date"].ToString()));
					dbObject.Description = dataTable.Rows[0]["Description"].ToString().Trim();
					dbObject.SetParentId(new Guid(dataTable.Rows[0]["ParentId"].ToString()));
					dbObject.SetDatabase(this.mDatabase);
					if (dataTable.Rows[0]["eID1"].ToString() != "")
					{
						dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[0]["eID1"].ToString());
					}
					if (dataTable.Rows[0]["eID2"].ToString() != "")
					{
						dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[0]["eID2"].ToString());
					}
					dbObject.ExData.Tag1 = dataTable.Rows[0]["tag1"].ToString();
					dbObject.ExData.Tag2 = dataTable.Rows[0]["tag2"].ToString();
					return dbObject;
				}
				return null;
			}
		}

		public DbObject this[string name]
		{
			get
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,CategoryId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where Name='" + name + "'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[0]["Img"]));
					DbObject dbObject = new DbObject(dataTable.Rows[0]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[0]["GUID"].ToString()));
					dbObject.Limit = Convert.ToBoolean(dataTable.Rows[0]["Limit"].ToString());
					dbObject.Version = dataTable.Rows[0]["Version"].ToString().Trim();
					dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[0]["Date"].ToString()));
					dbObject.Description = dataTable.Rows[0]["Description"].ToString().Trim();
					dbObject.SetParentId(new Guid(dataTable.Rows[0]["ParentId"].ToString()));
					dbObject.SetDatabase(this.mDatabase);
					if (dataTable.Rows[0]["eID1"].ToString() != "")
					{
						dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[0]["eID1"].ToString());
					}
					if (dataTable.Rows[0]["eID2"].ToString() != "")
					{
						dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[0]["eID2"].ToString());
					}
					dbObject.ExData.Tag1 = dataTable.Rows[0]["tag1"].ToString();
					dbObject.ExData.Tag2 = dataTable.Rows[0]["tag2"].ToString();
					return dbObject;
				}
				return null;
			}
		}

		public List<DbObject> this[Parent parent]
		{
			get
			{
				List<DbObject> list = new List<DbObject>();
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where ParentId='" + parent.Guid.ToString() + "'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
						DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString()));
						dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
						dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
						dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString()));
						dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
						dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString()));
						dbObject.SetDatabase(this.mDatabase);
						if (dataTable.Rows[i]["eID1"].ToString() != "")
						{
							dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
						}
						if (dataTable.Rows[i]["eID2"].ToString() != "")
						{
							dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
						}
						dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
						dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
						list.Add(dbObject);
					}
				}
				return list;
			}
		}

		public List<DbObject> GetAll()
		{
			List<DbObject> list = new List<DbObject>();
			if (this.mDatabase.dbCon.State == ConnectionState.Closed)
			{
				this.mDatabase.dbCon.Open();
			}
			DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable");
			if (this.mDatabase.dbCon.State == ConnectionState.Open)
			{
				this.mDatabase.dbCon.Close();
			}
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
					DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString()));
					dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
					dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
					dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString()));
					dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
					dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString()));
					dbObject.SetDatabase(this.mDatabase);
					if (dataTable.Rows[i]["eID1"].ToString() != "")
					{
						dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
					}
					if (dataTable.Rows[i]["eID2"].ToString() != "")
					{
						dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
					}
					dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
					dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
					list.Add(dbObject);
				}
			}
			return list;
		}

		public bool Remove(DbObject family, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from ObjectTable where GUID='" + family.Guid.ToString() + "'", tr);
				result = true;
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				result = false;
			}
			return result;
		}

		public List<DbObject> this[Parent parent, int start, int end]
		{
			get
			{
				List<DbObject> list = new List<DbObject>();
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where ParentId='" + parent.Guid.ToString() + "'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					for (int i = start; i < ((dataTable.Rows.Count > end) ? end : dataTable.Rows.Count); i++)
					{
						Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
						DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString()));
						dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
						dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
						dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString().Trim()));
						dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
						dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString().Trim()));
						dbObject.SetDatabase(this.mDatabase);
						if (dataTable.Rows[i]["eID1"].ToString() != "")
						{
							dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
						}
						if (dataTable.Rows[i]["eID2"].ToString() != "")
						{
							dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
						}
						dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
						dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
						list.Add(dbObject);
					}
				}
				return list;
			}
		}

		public List<DbObject> SearchFamily(string familyName, int start, int end)
		{
			List<DbObject> list = new List<DbObject>();
			try
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where Name like'%" + familyName + "%'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					for (int i = start; i < ((dataTable.Rows.Count > end) ? end : dataTable.Rows.Count); i++)
					{
						Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
						DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString()));
						dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
						dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
						dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString().Trim()));
						dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
						dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString().Trim()));
						dbObject.SetDatabase(this.mDatabase);
						if (dataTable.Rows[i]["eID1"].ToString() != "")
						{
							dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
						}
						if (dataTable.Rows[i]["eID2"].ToString() != "")
						{
							dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
						}
						dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
						dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
						list.Add(dbObject);
					}
				}
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
			}
			return list;
		}

		public List<DbObject> SearchFamily(string familyName)
		{
			List<DbObject> list = new List<DbObject>();
			try
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where Name like'%" + familyName + "%'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
						DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString().Trim()));
						dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
						dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
						dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString()));
						dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
						dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString().Trim()));
						dbObject.SetDatabase(this.mDatabase);
						if (dataTable.Rows[i]["eID1"].ToString() != "")
						{
							dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
						}
						if (dataTable.Rows[i]["eID2"].ToString() != "")
						{
							dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
						}
						dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
						dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
						list.Add(dbObject);
					}
				}
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
			}
			return list;
		}

		public int SearchFamilyCount(string familyName)
		{
			new List<DbObject>();
			int result = 0;
			try
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,KeyName,eID1,eID2,tag1,tag2 from ObjectTable where Name like'%" + familyName + "%'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					result = dataTable.Rows.Count;
				}
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
			}
			return result;
		}

		public List<DbObject> SearchFamily(DateTime startTime, DateTime endTime, int start, int end)
		{
			List<DbObject> list = new List<DbObject>();
			try
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where Date BETWEEN '{0}' AND '{1}'", startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd")));
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					for (int i = start; i < ((dataTable.Rows.Count > end) ? end : dataTable.Rows.Count); i++)
					{
						Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
						DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString()));
						dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
						dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
						dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString().Trim()));
						dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
						dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString().Trim()));
						dbObject.SetDatabase(this.mDatabase);
						if (dataTable.Rows[i]["eID1"].ToString() != "")
						{
							dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
						}
						if (dataTable.Rows[i]["eID2"].ToString() != "")
						{
							dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
						}
						dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
						dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
						list.Add(dbObject);
					}
				}
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
			}
			return list;
		}

		public List<DbObject> SearchFamily(DateTime startTime, DateTime endTime)
		{
			List<DbObject> list = new List<DbObject>();
			try
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where Date BETWEEN '{0}' AND '{1}'", startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd")));
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						Image img = new Bitmap(new MemoryStream((byte[])dataTable.Rows[i]["Img"]));
						DbObject dbObject = new DbObject(dataTable.Rows[i]["Name"].ToString().Trim(), img, new Guid(dataTable.Rows[i]["GUID"].ToString().Trim()));
						dbObject.Limit = Convert.ToBoolean(dataTable.Rows[i]["Limit"].ToString());
						dbObject.Version = dataTable.Rows[i]["Version"].ToString().Trim();
						dbObject.SetDate(Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString()));
						dbObject.Description = dataTable.Rows[i]["Description"].ToString().Trim();
						dbObject.SetParentId(new Guid(dataTable.Rows[i]["ParentId"].ToString().Trim()));
						dbObject.SetDatabase(this.mDatabase);
						if (dataTable.Rows[i]["eID1"].ToString() != "")
						{
							dbObject.ExData.Eid1 = Convert.ToInt32(dataTable.Rows[i]["eID1"].ToString());
						}
						if (dataTable.Rows[i]["eID2"].ToString() != "")
						{
							dbObject.ExData.Eid2 = Convert.ToInt32(dataTable.Rows[i]["eID2"].ToString());
						}
						dbObject.ExData.Tag1 = dataTable.Rows[i]["tag1"].ToString();
						dbObject.ExData.Tag2 = dataTable.Rows[i]["tag2"].ToString();
						list.Add(dbObject);
					}
				}
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
			}
			return list;
		}

		public int SearchFamilyCount(DateTime startTime, DateTime endTime)
		{
			new List<DbObject>();
			int result = 0;
			try
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select Img,`Limit`,Name,ParentId,`Version`,Date,Description,GUID,eID1,eID2,tag1,tag2 from ObjectTable where Date BETWEEN '{0}' AND '{1}'", startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd")));
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null)
				{
					result = dataTable.Rows.Count;
				}
			}
			catch
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
			}
			return result;
		}

		private RelationDatabase mDatabase;
	}
}
